<?php
/**
 * Created by PhpStorm.
 * User: Hello pan
 * Date: 9/19/25
 * Time: 5:31 PM
 */

namespace app\models;

use yii\elasticsearch\ActiveRecord;

class Customer extends ActiveRecord
{

    /**
     * Desc: 索引name
     * Auth: hello pan
     * Date: 9/19/25 5:44 PM
     * @return string
     */
    public static function index()
    {
        return 'customer';
    }

    public function attributes()
    {
        $mapping = self::mapping();
        return array_keys($mapping['properties']);
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'first_name' => '姓氏',
            'last_name' => '名字',
            'email' => '邮件',
            'order_ids' => '订单id',
            'registered_at' => '注册时间',
            'updated_at' => '更新时间',
            'status' => '状态',
            'is_active' => '是否活跃',
        ];
    }


    public static function mapping()
    {
        return [
            'properties' => [
                'id'             => ['type' => 'long'],// 数字类型 支持 long，integer，short，byte，double，float，half_float，scaled_float
                'first_name'     => ['type' => 'text'],
                'last_name'      => ['type' => 'text'],
                'order_ids'      => ['type' => 'keyword'],
                'email'          => ['type' => 'keyword'],
                'registered_at'  => [
                    'type' => 'date',
                    // 这里有很多类型
                    /**
                     * strict_date_optional_time, date_optional_time  通用的ISO日期格式，其中日期部分是必需的，时间部分是可选的。例如 "2015-01-01"或"2015/01/01 12:10:30"。
                     * epoch_millis  13位毫秒时间戳
                     * epoch_second  10位普通时间戳
                     */
                    'format' => 'yyyy-MM-dd HH:mm:ss||yyyy-MM-dd',
                ],
                'updated_at'     => ['type' => 'date','format' => 'yyyy-MM-dd HH:mm:ss||yyyy-MM-dd'],
                'status'         => ['type' => 'keyword'],
                'is_active'      => ['type' => 'boolean'],
            ]
        ];

    }

    /**
     * Set (update) mappings for this model
     */
    public static function updateMapping()
    {
        $db = static::getDb();
        $command = $db->createCommand();
        $command->setMapping(static::index(), static::type(), static::mapping());
    }

    /**
     * Create this model's index
     */
    public static function createIndex()
    {
        $db = static::getDb();
        $command = $db->createCommand();
        $command->createIndex(static::index(), [
            //'aliases' => [ /* ... */ ],
            'mappings' => static::mapping(),
            //'settings' => [ /* ... */ ],
        ]);
    }

    /**
     * Delete this model's index
     */
    public static function deleteIndex()
    {
        $db = static::getDb();
        $command = $db->createCommand();
        $command->deleteIndex(static::index(), static::type());
    }

    /**
     * Desc: 批量插入
     * Auth: hello pan
     * Date: 9/19/25 10:50 PM
     * @param array $data
     * @return mixed
     */
    public function batchInsert(array $data)
    {
        $db = static::getDb();
        $bulkCommand = $db->createBulkCommand();

        foreach ($data as $_item) {
            $actionMeta = [
                '_index' => static::index(),
//                '_type' => static::type(),
                '_id' => $_item['id'] // 自定义 ID
            ];
            $sourceData = $_item;

            $bulkCommand->addAction(['index' => $actionMeta], $sourceData); // 添加操作[1](@ref)
        }
        return $bulkCommand->execute();
    }

}